﻿package kizzo.datastruct 
{		
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.geom.Point;
	/**
	 * @private
	 */
	//import fl.motion.BezierSegment;

	public class Edge  
	{
		private var _start_vertex:Vertex;
		private var _end_vertex:Vertex;
		private var _weight:Number;
		
		public var sprite:Sprite;
		public var arrow:Sprite;
		public var text:TextField;
		
		private static const ARROW_ANGLE:Number = 10;
		private static const ANCHOR_HEIGHT:Number = 50;	
		private static const ARC_SEGMENTS:Number = 64;
		
		/*
		 * Creates a directed edge from start_vertex to end_vertex with weight weight.
		 */
		public function Edge(start_vertex:Vertex, end_vertex:Vertex, weight:Number) 
		{
			this._start_vertex = start_vertex;
			this._end_vertex = end_vertex;
			
			if (weight < 0)
				throw new Error("Edge weights must be non-negative");
			this._weight = weight;
			
			this.sprite = new Sprite();			
			draw_arrow();	
			draw_text();
			this.sprite.addChild(arrow);
			this.sprite.addChild(text);
		}
		
		/*
		 * Traces the edge in text format.
		 */
		public function traceEdge():void
		{
			trace(start_vertex.name + "->" + end_vertex.name + " W:" + this.weight);
		}
		
		public function get start_vertex():Vertex
		{
			return _start_vertex;
		}
		
		public function get end_vertex():Vertex
		{
			return _end_vertex;
		}
		
		public function get weight():Number
		{
			return _weight;
		}
		
		public function draw():void
		{						
			var diff_x:Number = this.end_vertex.pt.x - this.start_vertex.pt.x;
			var diff_y:Number = this.end_vertex.pt.y - this.start_vertex.pt.y;			
			
			sprite.graphics.clear();
			
			sprite.graphics.lineStyle(1, 0x3333cc, .4);
			sprite.graphics.lineTo(diff_x, diff_y);

			
			var angle:Number = - Math.atan2(diff_x, diff_y) + Math.PI/2;
			arrow.x = diff_x / 3;
			arrow.y = diff_y  / 3;
			arrow.rotation = (angle * 180) / Math.PI;
			text.x = diff_x  / 3;
			text.y = diff_y  / 3;
			
			sprite.graphics.beginFill(0x336699, 1);
			sprite.graphics.drawCircle(0, 0, 2);
			sprite.graphics.drawCircle(diff_x, diff_y, 2);
			sprite.graphics.endFill();
			
			sprite.mouseEnabled = false;			
		}
		
		
		/*public function draw_():void
		{
			var diff_x:Number = this.end_vertex.pt.x - this.start_vertex.pt.x;
			var diff_y:Number = this.end_vertex.pt.y - this.start_vertex.pt.y;			
			
			sprite.graphics.clear();
			
			sprite.graphics.lineStyle(1, 0x3333cc, .6);
			//sprite.graphics.lineTo(diff_x, diff_y);
			
			var angle:Number = - Math.atan2(diff_x, diff_y);			
			
			var height:Number = 0;//Point.distance(this.start_vertex.pt, this.end_vertex.pt)/10;
			var anchor_1_x:Number = diff_x / 4 + height * Math.cos(angle);
			var anchor_2_x:Number = 3 * diff_x / 4 + height * Math.cos(angle);
			var anchor_1_y:Number = diff_y / 4 + height * Math.sin(angle);
			var anchor_2_y:Number = 3 * diff_y / 4 + height * Math.sin(angle);
			
			var bezier_seg:BezierSegment = new BezierSegment(new Point(0,0), new Point(anchor_1_x,anchor_1_y), new Point(anchor_2_x,anchor_2_y), new Point(diff_x, diff_y));
			/*sprite.graphics.moveTo(diff_x/4, diff_y/4);
			sprite.graphics.lineTo(anchor_1_x, anchor_1_y);
			
			sprite.graphics.moveTo(diff_x*3/4, diff_y*3/4);
			sprite.graphics.lineTo(anchor_2_x, anchor_2_y);*/
			/*
			sprite.graphics.moveTo(0, 0);
			var arc_pt:Point;
			var step:Number = 2 / Point.distance(this.start_vertex.pt, this.end_vertex.pt);
			for (var t:Number = 0; t <= 1.0; t += step)
			{
				arc_pt = bezier_seg.getValue(t);
				sprite.graphics.lineTo(arc_pt.x, arc_pt.y);
				t += step*2;
				arc_pt = bezier_seg.getValue(t);
				sprite.graphics.moveTo(arc_pt.x, arc_pt.y);
			}
			
			arrow.x = bezier_seg.getValue(.5).x;
			arrow.y = bezier_seg.getValue(.5).y;
			arrow.rotation = (angle * 180) / Math.PI + 90;
			text.x = bezier_seg.getValue(.5).x;
			text.y = bezier_seg.getValue(.5).y;			
			
			
			sprite.graphics.beginFill(0x336699, 1);
			sprite.graphics.drawCircle(0, 0, 2);
			sprite.graphics.drawCircle(diff_x, diff_y, 2);
			sprite.graphics.endFill();
			
			sprite.mouseEnabled = false;			
		}*/
		
		
		private function draw_text():void
		{
			text = new TextField();			
			text.text = "W:" + this.weight;			
			var tf_format:TextFormat = new TextFormat("Tahoma", 9);
			text.setTextFormat(tf_format);
			text.x = 12;
			text.y = - 9;
			text.selectable = false;			
			text.mouseEnabled = false;
			text.filters = [new GlowFilter(0xffffff, .8, 2, 2, 6, 2)];
		}
		
		private function draw_arrow():void
		{
			arrow = new Sprite();
			
			arrow.graphics.beginFill(0x3333cc, .8);			
			arrow.graphics.moveTo( -12, 5);
			arrow.graphics.lineTo( 0, 0);
			arrow.graphics.lineTo( -12, -5);
			arrow.graphics.lineTo( -8, 0);
			arrow.graphics.lineTo( -12, 5);
			arrow.graphics.endFill();	
		}
		
		
	}
	
}